package LeetCode.Hash;

import java.util.*;

/**
 * @author : LdLtd
 * @Date : 2023/7/22
 * @Description:49. 字母异位词分组
 */
public class group_anagrams {
    public static void main(String[] args) {
        String a="123";
        String b="321";
        char[] a1 = b.toCharArray();
        Arrays.sort(a1);
        String s2 = new String(a1);
//        System.out.println(s2.hashCode());
//       System.out.println("123".hashCode());

        System.out.println("123".hashCode());
        System.out.println("213".hashCode());
//        System.out.println("2".hashCode());
//        System.out.println("3".hashCode());
//        System.out.println("a".hashCode());
//        System.out.println("b".hashCode());
    }
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            Map<String, List<String>> map = new HashMap<>();
            for (String str : strs) {
                String s = str.chars()
                        .sorted()
                        .collect(StringBuffer::new, StringBuffer::appendCodePoint, StringBuffer::append)
                        .toString();
                map.computeIfAbsent(String.valueOf(s.hashCode()), k -> new ArrayList<>()).add(str);
            }
           return new ArrayList<>(map.values());
        }
        /*别的写法*/
        public List<List<String>> groupAnagrams2(String[] strs) {
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            for (String str : strs) {
                char[] array = str.toCharArray();
                Arrays.sort(array);
                String key = new String(array);
                List<String> list = map.getOrDefault(key, new ArrayList<String>());
                list.add(str);
                map.put(key, list);
            }
            return new ArrayList<List<String>>(map.values());
        }

    }
}
